layui.config({
    base: '/home/js/util/'
}).use(['element','laypage','jquery','menu','table','form'],function(){
    element = layui.element,layer = layui.layer,
        $ = layui.$,form = layui.form;

    var searchParam = {
        req: {
            pid:'0'
        }
    };


    var loadData = function () {
        //加载父级菜单
        CoreUtil.sendAjax("/permission/getPermission", JSON.stringify(searchParam), function (res) {
            if (res.data && res.data.records != null) {
                rendData(res.data.records);
            } else {
                layer.msg('获取菜单信息失败', {icon: 2});
            }
        }, "POST", false, function (res) {
            layer.msg('站点出问题啦！请联系管理员', {icon: 6})
        });
    };

    var rendData = function (data){
        var html = '<select lay-verify="required"  name="pid" >\n' +
            '<option value="0" >无父级菜单</option>';
        $.each(data,function (index,value){
            html +='<option value="'+ value.permissionId +'">'+ value.name +'</option>'
        });
        html+= '            </select>';

        $("#pmenuid").html(html);
        form.render();//渲染
    };

    //监听提交
    form.on('submit(menuadd)',
        function(data) {
        // console.log(data);
            //发异步，提交数据
            $.ajax({
                url:'/permission/save',
                type:'POST',
                async:true,
                data: JSON.stringify(data.field),
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                contentType: "application/json",
                success:function(data){
                    if (data.code == "0"){
                        layer.msg("增加成功", {
                                icon: 6,
                                time:1000
                            },
                            function() {
                                //关闭当前frame
                                xadmin.close();

                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                    }else {
                        layer.alert(data.msg, {
                            icon: 5});
                    }
                }
            });

            return false;
        });

    loadData();
})
